<?php

namespace catchAdmin\phone\model;

use catcher\exceptions\AlertErrorException;

class WechatUserBank extends \catchAdmin\model\WechatUserBank
{
    /**
     * 保存
     */
    public function saveData($param)
    {
        if (!empty($param['id'])) {
            $msg = $this
                ->where('wechat_user_id', $this->getLoginId())
                ->where('id', $param['id'])
                ->find();

            if (empty($msg)) {
                throw new AlertErrorException('参数不正确');
            }
        }

        $this->startTrans();

        $msg = $this->saveDataCC([
            'id'             => $msg['id'] ?? 0,
            'wechat_user_id' => $this->getLoginId(),
            'bank_name'      => $param['bank_name'],
            'bank_kai_name'  => $param['bank_kai_name'],
            'bank_no'        => $param['bank_no'],
        ]);

        if ($this
                ->where('wechat_user_id', $msg['wechat_user_id'])
                ->where('bank_no', $msg['bank_no'])
                ->count() > 1) {
            throw new AlertErrorException('银行卡号不能重复');
        }

        $this->commit();

        return $msg;
    }


    /**
     * 删除
     */
    public function del($param)
    {
        $msg = $this
            ->where('wechat_user_id', $this->getLoginId())
            ->where('id', $param['id'])
            ->find();

        if (empty($msg)) {
            throw new AlertErrorException('参数不正确');
        }

        $msg->delete();

        return 1;
    }


    ######################################## 显示 ################################################


    /**
     * 列表
     */
    public function indexData($param)
    {
        return $this
            ->where('wechat_user_id', $this->getLoginId())
            ->order('id', 'desc')
            ->select();
    }


    /**
     * 列表
     */
    public function readData($param)
    {
        return $this
            ->where('wechat_user_id', $this->getLoginId())
            ->where('id', $param['id'])
            ->find();
    }

}
